package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzy.entity.Video;
import com.wzy.entity.Violence;
import com.wzy.mapper.VideoMapper;
import com.wzy.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.util.Const;
import com.wzy.util.Pager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Resource
    private VideoMapper videoMapper;
    /**
     * 查询视频，加入redis中，过期时间600s
     *
     * @param nums
     * @return
     */
    @Cacheable(key = "'videos'",value = "video")
    @Override
    public List<Video> getVideo(Integer nums) {

        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.last("limit " + nums);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean append(Video video) {
        return this.videoMapper.inserts(video);
    }

    @Override
    public boolean remove(Long videoId) {
        return this.videoMapper.delete(videoId);
    }

    @Override
    public boolean modify(Video video) {
        return this.videoMapper.update(video);
    }

    @Override
    public Video detail(Long videoId) {
        return this.videoMapper.selectById(videoId);
    }

    @Override
    public Pager<Video> list(Integer page) {
        List<Video> datas = this.videoMapper.selectbytime((page - 1) * Const.PAGE_SIZE, Const.PAGE_SIZE);
        int size = this.videoMapper.size();
        return new Pager<>(datas, size, page, Const.PAGE_SIZE);
    }

    @Override
    public List<Video> list() {
        return this.videoMapper.selectbytime(0, 0);
    }
}
